class Game{
    constructor(select,scoreEle,gameoverbg){
    this.startbtn=document.querySelector("#start")
    this.pausebtn=document.querySelector("#pause")

    this.gameoverimg=document.querySelector(gameoverbg)
    this.map=document.querySelector(select)
    this.scoreEle=document.querySelector(scoreEle)
    this.food=new Food(select)
         
    this.snake=new Snake(select)
    this.timer=0
    this.cunt=0
    }
    
    
     start(){
      this.num=0
      this.pausebtn.style.display="flex"
      this.startbtn.style.display="none"
        this.gameoverimg.style.display="none"
        this.timer= setInterval(() => {
            //死亡
            if(this.snake.isDie()){
               clearInterval(this.timer)
               this.gameover()
            }
            //动
            this.snake.move()
            //吃食物判断
            if(this.snake.isEat(this.food.x,this.food.y)){
                //变长
                this.snake.createHead()
                //食物更新
                this.food.foodPos()
                this.scorechange() 
            }
           
           
           if(this.num<=4500){
            this.num+=1
           }
          // console.log(this.num/10)
         }, (500-this.num));
     }
     pause(){
        clearInterval(this.timer)
        this.startbtn.style.display="flex"
        this.pausebtn.style.display="none"

     }
     restart(){
        this.startbtn.disabled=false
        window.location.reload()       
     }
     change(type){
        this.snake.direction=type
     }
     scorechange(){
      this.cunt++
      this.scoreEle.innerText=this.cunt
  }
  gameover(){
   this.gameoverimg.style.display="block"
   this.startbtn.disabled=true
   this.pausebtn.style.display="none"

  }
}